package action;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ModelDriven;

import entity.Account;
import entity.Sponsor;
import service.AccountDAO;
import service.SponsorDAO;
import service.impl.AccountDAOImpl;
import service.impl.SponsorDAOImpl;

public class SponsorAction extends SuperAction implements ModelDriven<Sponsor>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Sponsor sp = new Sponsor();
	
	
	public String register(){
		SponsorDAO sdao =  new SponsorDAOImpl();
		if(sdao.sponsorRegister(this.sp) )
		{
			return "register_success";
		}
		else {
			return "register_failure";
		}
	}
	
	public String login(){
		SponsorDAO sdao = new SponsorDAOImpl();
		Sponsor tempSp = sdao.sponsorLogin(sp);
		if(tempSp != null)
		{
			//session�б����û���¼��
			System.out.println(tempSp.getSponsorName());
			session.setAttribute("loginSponsor", tempSp);
			System.out.println(tempSp.getSponsorName());
			return "login_success";
		}
		else
		{
			System.out.println("login_failure");
			return "login_failure";
		}
	}
	
	@SkipValidation //该动作无需验证
	public String logout() {
		
		if(session.getAttribute("loginSponsor")!=null)
		{
			System.out.println("tuichulail");
			session.removeAttribute("loginSponsor");
			
		}
		return "logout_success";
	}
	
	public String update(){
		Sponsor	tempSp = (Sponsor)session.getAttribute("loginSponsor");
		sp.setSponsorId(tempSp.getSponsorId());
		
		SponsorDAO sdao = new SponsorDAOImpl();
		
		sdao.update(sp);
		session.removeAttribute("loginSponsor");
		session.setAttribute("loginSponsor", sp);
		System.out.println(sp.getSponsorEmail() + sp.getSponsorTel());
		return "update_success";
	}

	@Override
	public Sponsor getModel() {
		// TODO Auto-generated method stub
		return this.sp;
	}
}
